<template>
	<view class="add page c00">
		<view class="bgf7 p12">
			<view class="bgff px14 pb10">

				<view class="flex flexC borb py12">
					<text class="flex1">所属店铺</text>
					<view class="py3">{{selectShop.name}} </view>


				</view>

				<view class="flex flexC borb py12">
					<text class="flex1">包厢名字</text>
					<input v-model="name" class="aliR py3" placeholder="请输入包厢的名字" />
					<text class="arrowR ml3"></text>

				</view>
				
				<view class="flex flexC borb py12">
					<text class="flex1">包厢分类</text>
					<picker class="flex1" @change="bindPickerTypeChange" :value="index" :range="typeList">
						<view class="py3" style="text-align: right;">{{roomTypeValue}}</view>
					</picker>
					<text class="arrowR ml3"></text>
				</view>
				
				<view class="flex flexC borb py12">
					<text class="flex1">价格/小时</text>
					<input v-model="hour_price" type="digit" class="bgf6  aliR r40 px12 py3" placeholder="请输入价格" />元
					<text class="arrowR ml3"></text>

				</view>
				<view class="flex flexC borb py12">
					<text class="flex1">起订时间</text>
					<input v-model="min_book_hour" type="digit" class="py3 aliR" placeholder="请输入起订时间" />
					<text class="arrowR ml3"></text>
				</view>

				<picker @change="specPickerChange" :value="index" :range="array">
					<view class="flex flexC borb py12">
						<text class="flex1">包厢大小</text>
						<text class="py3" v-if="spec == 1">小包</text>
						<text class="py3" v-else-if="spec == 2">中包</text>
						<text class="py3" v-else-if="spec == 3">大包</text>

						<text class="arrowR ml3"></text>
					</view>
				</picker>

				<view class="flex flexC borb py12" @click="updateLogo">
					<text class="flex1">包厢LOGO</text>
					<image class="w48 " :src="logo_url" mode="aspectFill"></image>
					<text class="arrowR ml3"></text>
				</view>

				<view class="flex flexS borb pb12 pt18">
					<text class="mr14 pt3">包厢标签</text>
					<view class="flex flexC flexW flex1">
						<text@click="delTag(item)" v-for="(item,index) in tagList" :key="index"
							class="bgf6 mb6 r40 px12 py4 mr6">{{item}}</text>
							<text@click="addTag" class="bged mb6 r40 px15 py4">添加</text>
					</view>
				</view>
				<view class="pt12">
					<text class="mr14 pt3">包厢相册</text>
					<view class="flex flexC flexW mt12">
						<view class="imgbox r4 mr14 mb10" v-for="(item,index) in coverList" :key="index">
							<image class="r4" :src="item" mode="aspectFill"></image>
							<text @click="doRemoveCover(item)"
								class="bgf6 cff fs16 r50 pb2 boxS del flexC flex flexJC">×</text>
						</view>
						<view @click="addCover" class="imgbox c99 r4 mb10 fs30 flexC flex flexJC">+</view>
					</view>
				</view>
			</view>
			<view class="flex flexC flexJC py20">
				<view class="aliC py12 w-17 bg41 r40" @click="save">保存</view>
			</view>



		</view>



		<uni-popup ref="tagPop" type="bottom">
			<view class="popup-content2">
				<button class="popup-btn button" @click="doDelTag()">删除</button>

				<view class="popup-btn" @click="$refs.tagPop.close()">取消</view>
			</view>

		</uni-popup>

		<hFormAlert v-if="tagShow" title="添加标签" placeholder="请输入添加标签" @confirm="confirm" @cancel="cancel"></hFormAlert>


	</view>
</template>

<script>
	import hFormAlert from '@/components/h-form-alert/h-form-alert.vue';

	export default {
		components: {
			hFormAlert
		},
		data() {
			return {
				tagShow: false,
				shop_id: '',
				name: '',
				hour_price: '',
				min_book_hour: '',
				logo_path: '',
				tagList: [],
				coverList: [],
				coverPath: [],
				logo_url: '../../static/logo.jpeg',
				selectShop: null,
				spec: 1,
				array: ['小包(1~8人)', '中包(9~20)', '大包(21~30)'],
				roomTypeValue:'KTV',
				typeList:['KTV','棋牌'],

			};
		},
		onLoad(option) {
			this.shop_id = option.id;
			var select = option.selectShop;
			if (select != "") {
				this.selectShop = JSON.parse(select);
				this.shop_id = this.selectShop.id;
			}
		},

		onShow() {

		},
		methods: {
			specPickerChange(e) {
				var value = parseInt( e.detail.value);
				var index = value + 1;
				this.spec = index;
			},

			delTag(item) {
				this.currentTag = item;


				this.$refs.tagPop.open();
			},
			
			bindPickerTypeChange(e){
				this.roomTypeValue = e.detail.value;
			},

			doRemoveCover(item) {

				let list = new Array();
				let pathList = new Array();
				for (var i = 0; i < this.coverList.length; i++) {
					if (this.coverList[i] != item) {
						list.push(this.coverList[i]);
						pathList.push(this.coverPath[i]);
					}
				}
				this.coverPath = pathList;
				this.coverList = list;
			},


			doDelTag() {
				this.$refs.tagPop.close();
				let list = new Array();
				for (var i = 0; i < this.tagList.length; i++) {
					if (this.tagList[i] != this.currentTag) {
						list.push(this.tagList[i]);
					}
				}
				this.tagList = list;

			},

			confirm(e) {
				this.tagShow = false;
				console.info(e.text);
				if (this.tagList.length == 0) {
					this.tagList.push(e.text);
				} else {
					var isHave = false;
					for (var i = 0; i < this.tagList.length; i++) {
						if (this.tagList[i] == e.text) {
							isHave = true;
						}
					}
					if (!isHave) {
						this.tagList.push(e.text);
					}
				}

			},

			addTag() {
				this.tagShow = true;
			},

			cancel() {
				this.tagShow = false;
			},

			updateLogo() {
				let that = this;
				this.uploadType = "image";
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["camera", "album"], //从相册选择
					success: function(res) {
						//console.log(JSON.stringify(res.tempFilePaths));
						let images = res.tempFilePaths; //非APP平台不支持自定义压缩,暂时没有处理,可通过uni-app上传组件的sizeType属性压缩
						for (var i = 0; i < images.length; i++) {
							let tImg = images[i];
							that.upload(tImg, 'logo');
						}
					}
				});
			},

			addCover() {
				let that = this;
				this.uploadType = "image";
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["camera", "album"], //从相册选择
					success: function(res) {
						//console.log(JSON.stringify(res.tempFilePaths));
						let images = res.tempFilePaths; //非APP平台不支持自定义压缩,暂时没有处理,可通过uni-app上传组件的sizeType属性压缩
						for (var i = 0; i < images.length; i++) {
							let tImg = images[i];
							that.upload(tImg, 'cover');
						}
					}
				});
			},


			upload(img, type) {

				let that = this;
				uni.showLoading({
					title: "上传中"
				})
				uni.uploadFile({
					url: this.$uploadUrl,
					filePath: img,
					name: 'file',
					success: (uploadFileRes) => {

						uni.hideLoading();
						var res = uploadFileRes.data;

						// 根据接口具体返回格式   赋值具体对应url
						var url = JSON.parse(res).data.fullurl;
						var uploadUrl = JSON.parse(res).data.url;
						if (type == 'logo') {
							that.logo_url = url;
							that.logo_path = uploadUrl;
						} else {
							that.coverList.push(url);
							that.coverPath.push(uploadUrl)
						}
					}
				});

			},






			save() {

				if (this.name == '') {

					uni.showToast({
						title: "请填写包厢名",
						icon: 'none'
					})
					return;
				}


				if (this.hour_price == '') {

					uni.showToast({
						title: "请填写价格",
						icon: 'none'
					})
					return;
				}

				if (this.min_book_hour == '') {

					uni.showToast({
						title: "请填写起订时间",
						icon: 'none'
					})
					return;
				}

				if (this.tagList.length == 0) {

					uni.showToast({
						title: "请添加标签",
						icon: 'none'
					})
					return;
				}


				if (this.logo_path == "") {

					uni.showToast({
						title: "请上传logo",
						icon: 'none'
					})
					return;
				}

				if (this.coverPath.length == 0) {

					uni.showToast({
						title: "请上传封面",
						icon: 'none'
					})
					return;
				}
				
				var is_ktv = -1;
				if(this.roomTypeValue == 'KTV'){
					is_ktv = 1;
				}
				
				let param = {
					shop_id: this.shop_id,
					name: this.name,
					hour_price: this.hour_price,
					min_book_hour: this.min_book_hour,
					tag: JSON.stringify(this.tagList),
					logo: this.logo_path,
					cover: JSON.stringify(this.coverPath),
					spec:this.spec,
					is_ktv:this.is_ktv
					
				};
				let that = this;
				this.$api.addRoom(param).then(res => {

					if (res.code == 1) {
						uni.showModal({
							title: "添加提示",
							content: "添加成功",
							showCancel: false,
							complete() {
								uni.navigateBack({
									delta: 1
								})
							}
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		.w48 {
			width: 96rpx;
			height: 96rpx;
		}

		.imgbox {
			width: 164rpx;
			height: 164rpx;
			background-color: #E8E8E8;
			position: relative;

			image {
				width: 100%;
				height: 100%;
			}

			.del {
				width: 36rpx;
				height: 36rpx;
				background: rgba(181, 180, 180, .8);
				position: absolute;
				top: -12rpx;
				right: -12rpx;
			}
		}
	}

	.popup-content2 {
		background: #FFFFFF;

		.popup-btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
		}

		.button {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
			font-size: 11pt;
		}

		button::after {
			border: none;
		}

		.pt20 {
			border-bottom: 20rpx solid #f5f5f5;
		}
	}
</style>